using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIHealthBar : MonoBehaviour
{
    private Entity entity;
    private RectTransform rectTransform;
    private CharacterStats characterStats;
    private Slider slider;
    void Start()
    {
        rectTransform = GetComponent<RectTransform>();
        entity = GetComponentInParent<Entity>();
        characterStats = GetComponentInParent<CharacterStats>();
        slider = GetComponentInChildren<Slider>();
        entity.OnFliped += FlipUI;
        characterStats.OnHealthChanged += UpdateHealthBar;
        UpdateHealthBar();

    }

    private void FlipUI()
    { 
        rectTransform.rotation = Quaternion.identity;
    }

    private void UpdateHealthBar()
    {
        slider.value = characterStats.CurrentHealth /(float)characterStats.GetMaxHealth();
    }
   

    private void OnDisable()
    {
        entity.OnFliped -= FlipUI;
    }
}
